import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ModalController } from 'ionic-angular';

import { PublicCompoentsProvider } from '../../providers/public-compoents/public-compoents';
import { ApiProvider } from '../../providers/api/api'
import { Md5 } from 'ts-md5/dist/md5';
import { Storage } from '@ionic/storage';
// import { InAppBrowser } from '@ionic-native/in-app-browser';


/**
 * Generated class for the UserRechargePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
    selector: 'page-user-recharge',
    templateUrl: 'user-recharge.html',
})
export class UserRechargePage {
    public Arr: Array<number> = [ 55, 100, 300, 500, 800, 1000, 2000, 3000 ];
    public money:Number = 0;
    public chooseI:Number = 0;
    public payType:Number = 1;
    public other:any = '';
    public type: Array<any> = [
        {
            img:'./assets/imgs/recharge/wx.png', title:'微信支付', val:1, checked:true
        }, {
            img:'./assets/imgs/recharge/al.png', title:'支付宝支付', val:2, checked:false
        }, {
            img:'./assets/imgs/recharge/kf.png', title:'客服充值', val:3, checked:false
        }
    ];
    public uid: any;
    public resData: any = {};
    public ua = window.navigator.userAgent.toLowerCase();


    constructor(
        public navCtrl: NavController, 
        public navParams: NavParams,
        public pubCom: PublicCompoentsProvider,
        public Api: ApiProvider,
        public storage: Storage,
        public modalCtrl: ModalController,
        // private iab: InAppBrowser
         ) {

        storage.get('userId').then(
            (value: string) => {
                this.uid = value
            }
        );

        console.log( this.ua )

    }

    ionViewDidLoad() {
        console.log('ionViewDidLoad UserRechargePage');
        this.getRandomNum();
        console.log( '充值金额：'+this.money )
    }

    // 获取随机数
    getRandomNum() {
        for( let i = 0; i < this.Arr.length; i++ ) {
            this.Arr[i] = Number(this.Arr[i]) + Number( Math.floor( Math.random()*5 + Math.random()* -5 ));
            this.money = this.Arr[0];
        }
    };

    chooseMoney(item, index){
        this.chooseI = index;
        this.money = item;
    };

    chooseType( item ){
        console.log( `this.payType：${item}` )
    }

    onchange(val){
        console.log( `this.money： ${val}` );
        if( val > 3000 ) {
            this.other = 3000;
            this.money = this.other
        }
        this.money = val;
    }
    onFocus () {
        this.chooseI = 8;
        console.log( `this.money： ${this.other}` )
    }

    // 提交支付订单
    sub () {
        if ( this.money >= 45 ) {
            if( this.payType == 1 ) {
                this.PayWechat();
            } else if ( this.payType == 2 ) {
                this.PayAli();
            } else {
                const modal = this.modalCtrl.create('RechargeKfPicPage');
                modal.present();
            }
        } else {
            this.pubCom.toast('最小充值45元', 'bottom')
        }
    }

    PayAli () {
        let timeStamp = (new Date()).getTime();
        this.Api.aliPay({
            times: timeStamp,
            token: Md5.hashStr( 'HalfPriceMall' + timeStamp ),
            id: this.uid,
            money: this.money,
            ip: window.localStorage.getItem('ip')
        }).then(res=>{
            console.log( res )
            this.resData = res;
            if( this.resData.code == 1 ) {
                window.location.href = 'http://www.xxbet.cn/alipay.php?url='+encodeURI(this.resData.data.pay_url);
                // const browser = this.iab.create( this.resData.data.pay_url, '_system', 'location=yes' );
                // browser.show();
            } else {
                this.pubCom.toast(this.resData.msg, 'bottom')
            }
        })
    }
    PayWechat () {
        let timeStamp = (new Date()).getTime();
        this.Api.weChat({
            times: timeStamp,
            token: Md5.hashStr( 'HalfPriceMall' + timeStamp ),
            id: this.uid,
            money: this.money,
        }).then(res=>{
            this.resData = res;
            if( this.resData.code == 1 ) {
                window.location.href = this.resData.data.url;
            } else {
                this.pubCom.toast(this.resData.msg, 'bottom')
            }
        })
    };


}
